﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Security.Principal;
using System.Web.Security;
using WebAppMatrix.Extensions;

namespace WebAppMatrix.Security
{
    [Serializable]
    public class MatrixIdentity : IIdentity
    {
        #region IIdentity 成员

        public string AuthenticationType
        {
            //get { return "WebAppMatrix Identity"; }
            get;
            set;
        }

        private bool _isAuthenticated;

        /// <summary>
        /// 获取当前用户是否通过验证
        /// </summary>
        public bool IsAuthenticated
        {
            get { return _isAuthenticated; }
        }

        private string _name;

        /// <summary>
        /// 获取当前用户
        /// </summary>
        public string Name
        {
            get { return _name; }
        }

        public MatrixIdentity()
        {
            _name = string.Empty;
            _isAuthenticated = false;
        }

        public MatrixIdentity(string username, bool isAuthenticated)
        {
            _name = username;
            _isAuthenticated = isAuthenticated;
        }

        public MatrixIdentity(string username, string password)
        {
            if (username.IsNullOrWhiteSpace())
                throw new ArgumentNullException("username");

            if (password.IsNullOrWhiteSpace())
                throw new ArgumentNullException("password");

            if (!Membership.ValidateUser(username, password)) { return; }

            _isAuthenticated = true;
            _name = username;
        }

        #endregion
    }
}
